/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package VistaConexion;

import metodos.Bayes;
import metodos.BayesGUIcontroller;
import datos.Datos;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class frameBayes extends javax.swing.JFrame {

    
    private BayesGUIcontroller byg;
    private ArrayList<String> atributos;
    private ArrayList<Integer> auxiliar;
    private String consulta = "";
    
    public frameBayes(Datos data) {
        initComponents();
        
        atributos = new ArrayList<String>();
        auxiliar = new ArrayList<Integer>();
        byg = new BayesGUIcontroller(data);
        byg.actualizarNombreClase(labelClase);
        byg.actualizarValoresClase(listaValoresClase);
        byg.actualizarListaAtributos(listaAtributos);
        botonAñadir.setEnabled(false);
        
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaAtributos = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        labelClase = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        listaValoresClase = new javax.swing.JList();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        listaValoresAtributo = new javax.swing.JList();
        botonExaminarAtributo = new javax.swing.JButton();
        botonCalcular = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        areaResultados = new javax.swing.JTextArea();
        botonResetConsulta = new javax.swing.JButton();
        botonLimpiar = new javax.swing.JButton();
        botonAñadir = new javax.swing.JButton();

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jList2);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        listaAtributos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(listaAtributos);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("CLASE:");

        labelClase.setText("...");

        jLabel2.setText("VALORES CLASE");

        jScrollPane3.setViewportView(listaValoresClase);

        jLabel3.setText("ATRIBUTOS");

        jLabel4.setText("VALORES ATRIBUTO");

        listaValoresAtributo.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane4.setViewportView(listaValoresAtributo);

        botonExaminarAtributo.setText(">>>");
        botonExaminarAtributo.setToolTipText("Muestra los valores que puede tomar el atributo seleccionado");
        botonExaminarAtributo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonExaminarAtributoActionPerformed(evt);
            }
        });

        botonCalcular.setText("CALCULAR");
        botonCalcular.setToolTipText("Arroja el resultado del cálculo");
        botonCalcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCalcularActionPerformed(evt);
            }
        });

        jLabel5.setText("RESULTADOS");

        areaResultados.setColumns(20);
        areaResultados.setRows(5);
        jScrollPane5.setViewportView(areaResultados);

        botonResetConsulta.setText("RESET CONSULTA");
        botonResetConsulta.setToolTipText("Define nuevamente los parámetros de la consulta");
        botonResetConsulta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonResetConsultaActionPerformed(evt);
            }
        });

        botonLimpiar.setText("LIMPIAR TODO");
        botonLimpiar.setToolTipText("Limpia el área de resultados y reinicia el proceso");
        botonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonLimpiarActionPerformed(evt);
            }
        });

        botonAñadir.setText("Añadir");
        botonAñadir.setToolTipText("Añade el valor del atributo seleccionado a la consulta");
        botonAñadir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAñadirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(labelClase, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(51, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addComponent(jLabel3)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(botonExaminarAtributo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(botonAñadir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(49, 49, 49))))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addGap(64, 64, 64))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSeparator1)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(botonResetConsulta)
                .addGap(18, 18, 18)
                .addComponent(botonCalcular)
                .addGap(18, 18, 18)
                .addComponent(botonLimpiar)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator2)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(179, 179, 179)
                .addComponent(jLabel5)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelClase)
                            .addComponent(jLabel1)))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1)
                            .addComponent(jScrollPane4)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addComponent(botonExaminarAtributo)
                        .addGap(35, 35, 35)
                        .addComponent(botonAñadir)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(botonCalcular)
                            .addComponent(botonResetConsulta)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(botonLimpiar)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonCalcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCalcularActionPerformed
        
        if(atributos.isEmpty()){
            JOptionPane.showMessageDialog(null, "No ha construido la consulta", "ERROR", JOptionPane.ERROR_MESSAGE);
            consulta = "";
            auxiliar.clear();
        }
        else{
            botonCalcular.setEnabled(false);
            botonResetConsulta.setEnabled(false);
            areaResultados.append("\n-------------------------\n");
            byg.calcular(areaResultados, atributos);
        }
        
    }//GEN-LAST:event_botonCalcularActionPerformed

    private void botonExaminarAtributoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonExaminarAtributoActionPerformed
        if(listaAtributos.getSelectedIndex() != -1){
            byg.actualizarValoresAtributos(listaValoresAtributo, listaAtributos.getSelectedIndex());
            botonAñadir.setEnabled(true);
        }
        else
           JOptionPane.showMessageDialog(null, "Debe seleccionar un atributo", "ERROR", JOptionPane.ERROR_MESSAGE);
    }//GEN-LAST:event_botonExaminarAtributoActionPerformed

    private void botonAñadirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAñadirActionPerformed
        // TODO add your handling code here:
        
        boolean bandera = false;
        
        
        if(listaValoresAtributo.getSelectedIndex() == -1)
            JOptionPane.showMessageDialog(null, "Debe seleccionar el valor"
                    + " de un atributo", "ERROR", JOptionPane.ERROR_MESSAGE);
        else{
            
            if(atributos.isEmpty()){
                atributos.add(String.valueOf(listaValoresAtributo.getSelectedValue()));
                consulta += String.valueOf(listaValoresAtributo.getSelectedValue());
                consulta += " -> ";
                areaResultados.append("Consulta: \n");
                areaResultados.append(consulta);
                auxiliar.add(listaAtributos.getSelectedIndex());
                botonAñadir.setEnabled(false);
            }
            
           else{
                
                for (int i = 0; i < auxiliar.size(); i++) {
                    if(listaAtributos.getSelectedIndex() == auxiliar.get(i))
                        bandera = true;
                }
                
                if(bandera)
                    JOptionPane.showMessageDialog(null, "Ya este valor ha sido"
                            + " seleccionado", "ERROR", JOptionPane.ERROR_MESSAGE);
                else{
                    atributos.add(String.valueOf(listaValoresAtributo.getSelectedValue()));
                    auxiliar.add(listaAtributos.getSelectedIndex());
                    consulta = String.valueOf(listaValoresAtributo.getSelectedValue());
                    consulta += " -> ";
                    areaResultados.append(consulta);
                    botonAñadir.setEnabled(false);
                }
            }
        }
        
        
    }//GEN-LAST:event_botonAñadirActionPerformed

    private void botonResetConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonResetConsultaActionPerformed
        // TODO add your handling code here:
        consulta = "";
        auxiliar.clear();
        atributos.clear();
        areaResultados.setText("");
        botonAñadir.setEnabled(false);
    }//GEN-LAST:event_botonResetConsultaActionPerformed

    private void botonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonLimpiarActionPerformed
        // TODO add your handling code here:
        botonCalcular.setEnabled(true);
        botonResetConsulta.setEnabled(true);
        consulta = "";
        auxiliar.clear();
        atributos.clear();
        areaResultados.setText("");
        botonAñadir.setEnabled(false);
    }//GEN-LAST:event_botonLimpiarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea areaResultados;
    private javax.swing.JButton botonAñadir;
    private javax.swing.JButton botonCalcular;
    private javax.swing.JButton botonExaminarAtributo;
    private javax.swing.JButton botonLimpiar;
    private javax.swing.JButton botonResetConsulta;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel labelClase;
    private javax.swing.JList listaAtributos;
    private javax.swing.JList listaValoresAtributo;
    private javax.swing.JList listaValoresClase;
    // End of variables declaration//GEN-END:variables
}
